
function __trapSignals() {

	## ----- head -----
	##
	## DESCRIPTION:
	##   trap function for script signals
	##
	## ARGUMENTS:
	##   1: signal (req): signal that was trapped
	##
	## GLOBAL VARIABLES USED:
	##   /
	##

	local signal=${1}
	if [[ -z "${signal}" ]]; then
		__msg err "argument 1 (signal) missing"
		return 2 # error
	fi
	__msg debug "signal: ${signal}"

	## ----- main -----

	## check for a user-defined trap function and call it
	if declare -F __trap${signal} &>/dev/null; then
		__trap${signal}
		return ${?}
	fi

	## default: generate a message and die on certain signals
	local -i die=0
	case ${signal} in
		 SIGHUP) die=1; msg="received hangup signal" ;;
		 SIGINT) die=1; msg="received interrupt from keyboard" ;;
		SIGQUIT) die=1; msg="received quit from keyboard" ;;
		SIGABRT) die=1; msg="received abort signal" ;;
		SIGPIPE) die=1; msg="broken pipe" ;;
		SIGALRM) die=1; msg="received alarm signal" ;;
		SIGTERM) die=1; msg="received termination signal" ;;
		      *) die=0; msg="trapped signal ${signal}" ;;
	esac
	if [[ ${die} -ne 0 ]]; then
		__msg err "${msg}"
		exit 2 # error
	else
		__msg notice "${msg}"
	fi

	return 0 # success

} # __trapSignals()

## trap certain signals using __trapSignals()
declare -a __TrapSignals=(
	SIGHUP  # 1
	SIGINT  # 2 (^C)
	SIGQUIT # 3 (^\)
	SIGABRT # 6
	SIGPIPE # 13
	SIGALRM # 14
	SIGTERM # 15
)
for signal in "${__TrapSignals[@]}"; do
	trap "__trapSignals ${signal}" "${signal}"
done
